package gui;

import gui.AbstractViewPane.ViewMode;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SpringLayout;

import business.StatutEnseignantBL;
import business.TypeEnseignementBL;
import business.TypeHeureBL;

public class PanelEnseignementUIConsulter extends JFrame {
	
	
	protected JFrame masterClass = this;
	
	
	private int id = -1;
	private String nom = "";
	private float equiv = 1;
	private ViewMode currentViewMode;
	private PanelEnseignementUI panelSource;
	private JTextField textFieldNom;
	private JSpinner spin;
	private JPanel general;
	private JButton save;
	private JButton annuler;

	//methodes de récupperation des éléments des champs
	public int getId(){
		return id;
	}
	public String getNom(){
		return textFieldNom.getText();
	}
	public float getEquiv(){
		return Float.parseFloat(spin.getValue().toString());
	}
    
    
	public void drawCommandBox(JPanel commandBox) {
        JPanel buttonPanel = new JPanel(new BorderLayout());
        buttonPanel.add(new JSeparator(), BorderLayout.PAGE_START);
        buttonPanel.add(commandBox, BorderLayout.LINE_END);
        
        general.add(buttonPanel, BorderLayout.SOUTH);
	}
	
	public PanelEnseignementUIConsulter(ViewMode v, int idTypeEnseignement, PanelEnseignementUI panelS) {
		
		currentViewMode = v;
		panelSource = panelS;
		
    	setSize(500, 300);
		setTitle("Gestion des types d'enseignement");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		general = new JPanel();
		general.setLayout(new BorderLayout());
		
		general.setBorder(BorderFactory.createCompoundBorder(
		BorderFactory.createTitledBorder(getTitle()),
		BorderFactory.createEmptyBorder(5,5,5,5)));

		if(idTypeEnseignement != -1){
			TypeEnseignementBL typeEns = new TypeEnseignementBL(idTypeEnseignement);
			id = idTypeEnseignement;
			nom = typeEns.getNomTypeEnseignement();
			equiv = typeEns.getNombreHeuresEquivalentTD();
		}
		
				
        //Create and populate the panel.
        JPanel p = new JPanel(new SpringLayout());
        
        JLabel l = new JLabel("Nom:", JLabel.TRAILING);
        p.add(l);
        textFieldNom = new JTextField (nom);
        l.setLabelFor(textFieldNom);
        p.add(textFieldNom);
        
        l = new JLabel("Equivalent TD:", JLabel.TRAILING);
        p.add(l);
        SpinnerModel sm = new SpinnerNumberModel(equiv, 0, 10, 0.25);
        spin = new JSpinner(sm);
        l.setLabelFor(spin);
        p.add(spin);

        //Lay out the panel.
        SpringUtilities.makeCompactGrid(p,
                                        2, 2, //rows, cols
                                        6, 6,        //initX, initY
                                        6, 6);       //xPad, yPad
        
        general.add(p, BorderLayout.NORTH);
        add(general);
        
    	// Mode modification - afficher les boutons
        JPanel commandBox = new JPanel();
        save = new JButton("Enregistrer");
        save.setActionCommand("Enregistrer");
        save.addActionListener(new PanelTypeEnsListener());
        commandBox.add(save);
        
        annuler = new JButton("Annuler");
        annuler.setActionCommand("Annuler");
        annuler.addActionListener(new PanelTypeEnsListener());
        commandBox.add(annuler);
        drawCommandBox(commandBox);
	}

	private class PanelTypeEnsListener implements ActionListener {
		
		public void actionPerformed(ActionEvent e) {		
			if (e.getActionCommand().equals("Enregistrer")) {
				TypeEnseignementBL typeSave = new TypeEnseignementBL();
				ArrayList<String> listeErreur;
				// si -1, c'est un ajout d'un type heure, sinon modification du
				// type heure de l'id concerné
				if (id == -1) {
					System.out.println("demande ajout");
					listeErreur = typeSave.ajouterTypeEnseignement(getId(), getNom(), getEquiv());
					if(!listeErreur.isEmpty()){
						new MessageErrorUI(masterClass, listeErreur);
					} else {
						dispose();
					}
				} else {
					System.out.println("demande modif");
					listeErreur = typeSave.modifierTypeEnseignement(getId(), getNom(), getEquiv());
					if(!listeErreur.isEmpty()){
						new MessageErrorUI(masterClass, listeErreur);
					} else {
						dispose();
					}
				}
				panelSource.refreshListe();
				
				
			} else if (e.getActionCommand().equals("Annuler")) {
				System.out.println("annulation");
				dispose();
			}
		}
	}
}
